#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=16, maxm=16;

int max(int a, int b) {
	return a>b ? a : b;
}

int main() {
	freopen("convolution.in", "r", stdin);
	freopen("convolution.out", "w", stdout);

	static int a[1<<maxn], b[1<<maxn], f[maxm][maxm], c[1<<maxn];
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i=0; i<(1<<n); i++) {
		scanf("%d", a+i);
	}
	for (int i=0; i<(1<<n); i++) {
		scanf("%d", b+i);
	}
	for (int i=0; i<m; i++) {
		for (int j=0; j<m; j++) {
			scanf("%d", &f[i][j]);
		}
	}

	for (int i=0; i<(1<<n); i++) {
		for (int j=0; j<(1<<n); j++) {
			c[i|j] = max(c[i|j], f[a[i]][b[j]]);
		}
	}

	for (int i=0; i<(1<<n); i++) {
		printf("%d ", c[i]);
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}
